var txtPrizeCount = document.querySelector('.container .prizearea .prizecontent .prizsesize3 span');
var btnPirze = document.querySelector('.container .prizearea .prizecontent .prizsesize3 img:last-child');
var prizeList = document.querySelectorAll('.container .prizearea .prizecontent .prizestyle');

var prizeSize = prizeList.length;
var curChekcedPosi = 0;
var canPrizeNum = 5;

function init() {
  prizeList = Array.prototype.slice.call(prizeList);
  
  prizeList.sort(function (a, b) {
    var aNum = a.getAttribute('data-id');
    var bNum = b.getAttribute('data-id');
    return aNum - bNum;
  });
  console.log(prizeList);
  initView();
  initEvent();
}
function initView() {
  // 初始化显示
  txtPrizeCount.innerText = '还可以抽奖' + canPrizeNum + '次';
}

function initEvent() {
  btnPirze.addEventListener('click', function () {
    if (canPrizeNum <= 0) {
      alert('抽奖次数已用完');
      return;
    }

    startPrize();
  });
}

function startPrize() {
  var baseNum = 2 * prizeSize;
  var randomNum = getRandom(baseNum, baseNum + prizeSize);
  checkSinglePrize(0, randomNum);
}

function checkSinglePrize(loopnum, totalNum) {
  setTimeout(() => {
    loopnum++;
    if (loopnum >= totalNum) {
      return;
    }
    var lastCheck = document.querySelector('.container .prizearea .prizecontent .prizestyle.active');
    if (lastCheck) {
      lastCheck.classList.remove('active');
    }

    var curPrize = loopnum % prizeSize;
    console.log(curPrize);
    prizeList[curPrize].classList.add('active');

    checkSinglePrize(loopnum, totalNum);
  }, 200);
}

function getRandom(min, max) {
  max += 1;
  return Math.floor(Math.random() * (max - min) + min);
}
init();
